# This Python file uses the following encoding: utf-8
from django.db import models
from cicles.models import Cicle
from django.template.defaultfilters import slugify

class ModulProfessional(models.Model):
    cicle = models.ForeignKey( Cicle, db_index = True )
    numero = models.IntegerField(u"Número",  help_text = u'''Número del Mòdul Professional. Ex: 3 per a MP03''', blank = False ) 
    nom = models.CharField(u"Nom", max_length=250, help_text = u'''Nom del MP''', blank = False ) 
    logo = models.ImageField(u"Logo", blank=True, null=True, upload_to="moduls", help_text = u"Logo de la Unitat Formativa"  )
    slug = models.SlugField(db_index = True, unique = True)
    
    def save(self, *args, **kwargs):
        self.slug = self.slug if self.slug else self.my_slug()
        super(ModulProfessional, self).save(*args, **kwargs)
        
    def my_slug(self):
        slug_cicle =  self.cicle.codi if self.cicle else ''
        slug_txt = slug_cicle + '-' + unicode( self ) if slug_cicle else ''
        return slugify( slug_txt )
        
    class Meta:
        ordering=['cicle','numero']
        db_table = u'moduls_professionals'
        verbose_name = u'Mòdul Professional'
        verbose_name_plural = u'Mòduls Professional'
        
    def __unicode__(self):
        return u"MP-{0}".format( self.numero )
    
class UnitatFormativa(models.Model):
    modul_professional = models.ForeignKey( ModulProfessional, db_index = True )
    numero = models.IntegerField(u"Número", help_text = u'''Número de Unitat Formativa. Ex: 3 per a UF3''', blank = False ) 
    nom = models.CharField(u"Nom", max_length=250, help_text = u'''Nom de la Unitat Formativa''', blank = False ) 
    logo = models.ImageField(u"Logo", blank=True, null=True, upload_to="unitats", help_text = u"Logo de la Unitat Formativa"  )
    slug = models.SlugField(db_index = True, unique = True)
    
    def save(self, *args, **kwargs):
        self.slug = self.slug if self.slug else self.my_slug()
        super(UnitatFormativa, self).save(*args, **kwargs)
        
    def my_slug(self):
        slug_mp = self.modul_professional.my_slug( )
        slug_txt = slug_mp + '-' + u'UF{0}'+format( self.numero ) if slug_mp else ''
        return slugify( slug_txt )

    class Meta:
        ordering=['modul_professional','numero']
        db_table = u'unitats_formatives'
        verbose_name = u'Unitat Formativa'
        verbose_name_plural = u'Unitats Formativa'
        
    def __unicode__(self):
        return u"UF-{0}".format( self.numero )

class Contingut(models.Model):
    unitat_formativa = models.ForeignKey( UnitatFormativa, db_index = True )
    codi = models.CharField(u"Codi", max_length=10,  help_text = u'''Codi de contingut.''', blank = False ) 
    descripcio = models.CharField(u"Descripció", max_length=250, help_text = u'''Descripció del contingut''', blank = False ) 

    class Meta:
        ordering=['codi']
        db_table = u'continguts'
        verbose_name = u'Contingut'
        verbose_name_plural = u'Continguts'
        
    def __unicode__(self):
        return u"{0} - {1}".format( self.codi, self.descripcio )
    
